#include "boundary_val.h"

void boundaryvalues(
                    int imax,
                    int jmax,
                    double **U,
                    double **V,
                    int wl,
                    int wr,
                    int wt,
                    int wb,
                    char *problem,
                    int **Flag,
                    double driving_velocity
		   )
{
	int i,j;

	/* Left edge */
	switch (wl) {
		case NO_SLIP:
			for ( j = 1; j <= jmax ; j++) {
				U[0][j] = 0;
				V[0][j] = - V[1][j];
			}
			break;

		case FREE_SLIP:
			for ( j = 1; j <= jmax ; j++) {
				U[0][j] = 0;
				V[0][j] = V[1][j];
			}
			break;

		case OUTFLOW:
			for ( j = 1; j <= jmax ; j++) {
				U[0][j] = U[1][j];
				V[0][j] = V[1][j];
			}
			break;

		default:
			break;
	}

	/* Right edge */
	switch (wr) {
		case NO_SLIP:
			for (j = 1; j <= jmax ; j++) {
				U[imax][j] = 0;
				V[imax + 1][j] = - V[imax][j];
			}
			break;

		case FREE_SLIP:
			for ( j = 1; j <= jmax ; j++) {
				U[imax][j] = 0;
				V[imax + 1][j] = V[imax][j];
			}
			break;

		case OUTFLOW:
			for ( j = 1; j <= jmax ; j++) {
				U[imax][j] = U[imax-1][j];
				V[imax + 1][j] = V[imax][j];
			}
			break;

		default:
			break;
	}

	/* Top edge */
	switch (wt) {
		case NO_SLIP:
			for (i = 1; i <= imax; i++) {
				U[i][jmax + 1] = - U[i][jmax];
				V[i][jmax] = 0;
			}
			break;

		case FREE_SLIP:
			for ( i = 1; i <= imax; i++) {
				U[i][jmax + 1] =  U[i][jmax];
				V[i][jmax] = 0;
			}
			break;

		case OUTFLOW:
			for ( i = 1; i <= imax; i++) {
				U[i][jmax + 1] = U[i][jmax];
				V[i][jmax] = V[i][jmax - 1];
			}
			break;

		default:
			break;
	}

	/* Bottom edge */
	switch (wb) {
		case NO_SLIP:
			for (i = 1; i <= imax; i++) {
				U[i][0] = - U[i][1];
				V[i][0] = 0;
			}
			break;

		case FREE_SLIP:
			for ( i = 1; i <= imax; i++) {
				U[i][0] = U[i][1];
				V[i][0] = 0;
			}
			break;

		case OUTFLOW:
			for ( i = 1; i <= imax; i++) {
				U[i][0] = U[i][1];
				V[i][0] = V[i][1];
			}
			break;

		default:
			break;
	}

	/* Handle the edges */
    	for ( i = 1; i <= imax; i++ ) {
		for ( j = 1; j <= jmax; j++ ) {
			switch (Flag[i][j]) {
				case B_N:
					U[i][j] = - U[i][j + 1];
					U[i - 1][j] = U[i - 1][j + 1];
					V[i][j] = 0;
					break;
				case B_W:
					U[i - 1][j] = 0;
					V[i][j] = - V[i - 1][j];
					V[i][j - 1] = - V[i - 1][j -1];
					break;
				case B_S:
					U[i][j] = - U[i][j - 1];
					U[i - 1][j] = - U[i - 1][j - 1];
					V[i][j - 1] = 0;
					break;
				case B_E:
					U[i][j] = 0;
					V[i][j] = - V[i + 1][j];
					V[i][j - 1] = - V[i + 1][j - 1];
					break;
				case B_SW:
					U[i - 1][j] = 0;
					U[i][j] = - U[i][j - 1];
					V[i][j - 1] = 0;
					V[i][j] = - V[i - 1][j];
					break;
				case B_SE:
					U[i][j] = 0;
					U[i - 1][j] = - U[i - 1][j - 1];
					V[i][j - 1] = 0;
					V[i][j] = - V[i + 1][j];
					break;
				case B_NE:
					U[i][j] = 0;
					U[i - 1][j] = - U[i - 1][j + 1];
					V[i][j] = 0;
					V[i][j - 1] = - V[i + 1][j - 1];
					break;
				case B_NW:
					U[i - 1][j] = 0;
					U[i][j] = - U[i][j + 1];
					V[i][j] = 0;
					V[i][j - 1] = - V[i - 1][j - 1];
					break;
				default:
					break;
			}
		}
	}

	spec_boundary_val( problem, imax, jmax, U, V, driving_velocity);

}


void spec_boundary_val(
			char *problem,
			int imax,
			int jmax,
			double **U,
			double **V,
			double driving_velocity
			)
{
	int i,j;

	/* Cavity */
	if ( strncmp(problem,CAVITY,strlen(CAVITY)) == 0 ) {
		for ( i = 1; i < (imax + 1) ; i++) {
			U[i][jmax + 1] = 2.0 * driving_velocity - U[i][jmax];
		}
	}

	/* Karman */
	if ( strncmp(problem,KARMAN,strlen(KARMAN)) == 0 ) {
		for ( j = 1; j <= (jmax + 1) ; j++) {
			U[0][j] = driving_velocity;
			V[0][j] = - V[1][j];
		}
	}

	/* Plane */
	if ( strncmp(problem,PLANE,strlen(PLANE)) == 0 ) {
		/* do nothing */
	}

	/* Step */
	if ( strncmp(problem,STEP,strlen(STEP)) == 0 ) {
		for ( j = 1; j <= (jmax + 1) ; j++) {
			U[0][j] = driving_velocity;
			V[0][j] = - V[1][j];
		}
	}
	/* Nozzle */
	if ( strncmp(problem,NOZZLE,strlen(NOZZLE)) == 0 ) {
		/*for ( j = 1; j <= (jmax + 1) ; j++) {
			U[0][j] = driving_velocity;
			V[0][j] = - V[1][j];
		}*/
	}

	/* New Plane */
	if ( strncmp(problem,REVERSE_FLOW,strlen(REVERSE_FLOW)) == 0 ) {
		for ( i = 1; i < (imax + 1) ; i++) {
			U[i][jmax + 1] = 2.0 * driving_velocity - U[i][jmax];
		}
	}

}
